package com.aboverock.autoconfigure.config;

import com.aboverock.autoconfigure.properties.JwtProperties;
import com.aboverock.autoconfigure.properties.ProjectProperties;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger REST API 自动生成工具 访问路径: http://x.x.x.x/swagger-ui.html
 *
 * @author Rock Wang
 */
@Configuration
@EnableSwagger2
@EnableConfigurationProperties({ JwtProperties.class, ProjectProperties.class })
public class SwaggerConfig {
    
    private static final Logger logger = LoggerFactory.getLogger(SwaggerConfig.class);

    @Autowired
    private JwtProperties jwtProperties;

    @Autowired
    private ProjectProperties projectProperties;

    /**
     * Creates the rest api.
     *
     * @return the docket
     */
    @Bean
    public Docket createRestApi() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name(jwtProperties.getHeader())
                .description("shiro authentication token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false);
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(tokenPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.aboverock"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameters);
    }

    private ApiInfo apiInfo() {
        String projectName = projectProperties.getFinalName();
        if (null == projectName) {
            projectName = projectProperties.getArtifactId();
        }
        if(StringUtils.isEmpty(projectName)) {
            logger.warn("The setting of 'data-smart.project' for Swagger api is not set! ");
        }
        return new ApiInfoBuilder()
                .title(projectName + " Rest Api Document")
                .description(projectName + " Rest API")
                .version(projectProperties.getVersion())
                .build();
    }
}
